package org.byqh.dqeai.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.byqh.dqeai.common.Json;
import org.byqh.dqeai.dto.SysFavoriteDTO;
import org.byqh.dqeai.entity.SysFavorite;
import org.byqh.dqeai.entity.proxy.SysFavoriteProxy;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/sysFavorite")
@Tag(name = "系统[SysFavorite] - 收藏管理")
public class SysFavoriteController extends BaseController<SysFavorite, SysFavoriteProxy> {
    /**
     * 分页查询数据
     */
    @PostMapping(value = "/list")
    public Json<List<SysFavorite>> listAll(@RequestBody(required = false) SysFavoriteDTO dto) {

        return this.listImpl(SysFavorite.class, (query) -> {
            if (dto != null) {
                if (!StringUtils.isBlank(dto.getUserId())) {
                    query = query.where(o -> o.userId().eq(dto.getUserId()));
                }

                if (!StringUtils.isBlank(dto.getSharedUserId())) {
                    String subquery = String.format("select user_id from sys_favorite\n" +
                                    "where fav_id in(\n" +
                                    " select fav_id from SYS_FAVORITE_SHARE t \n" +
                                    "   where user_id='%s'\n" +
                                    "   or role_id in (\n" +
                                    "    select role_id from eai_sys_user_role where user_id='%s'\n" +
                                    "   )\n" +
                                    ") and user_id != '%s' ",
                            dto.getSharedUserId(),
                            dto.getSharedUserId(),
                            dto.getSharedUserId()
                    );

                    query = query.where(o -> o.userId().in(easyQueryClient.queryable(subquery, String.class)));
                }
            }

            return query;
        });
    }

    /**
     * 编辑多条记录
     */
    @PostMapping(value = "/editList")
    public Json<Boolean> EditList(@RequestBody SysFavoriteDTO dto) {
        if (dto.getItems() != null) {
            return this.updateBatch(dto.getItems());
        }

        return Json.error();
    }

    /**
     * 增加多条记录
     */
    @PostMapping(value = "/addList")
    public Json<Boolean> AddList(@RequestBody SysFavoriteDTO dto) {
        if (dto.getItems() != null) {
            return this.saveBatch(dto.getItems());
        }

        return Json.error();
    }

    @PostMapping(value = "/delete")
    public Json<Boolean> delete(@RequestBody SysFavoriteDTO dto) {
        try {
            if (dto.getIdList() == null || dto.getIdList().isEmpty()) {
                return Json.error();
            }

            long rows = this.getEntityDeletable()
                    .where(s -> s.favId().in(dto.getIdList()))
                    .executeRows();

            return rows > 0 ? Json.success() : Json.error();
        } catch (Exception ex) {
            return Json.exception(ex);
        }
    }
}